package com.yy.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.IdUtil;

import com.yy.common.constant.PmsConstants;
import com.yy.common.exception.ServiceException;
import com.yy.common.utils.DateUtils;
import com.yy.common.utils.SecurityUtils;
import com.yy.domain.PayPrestore;
import com.yy.domain.PayPrestoreAccount;
import com.yy.mapper.PayPrestoreAccountMapper;
import com.yy.mapper.PayPrestoreMapper;
import com.yy.service.IPayPrestoreService;
import com.yy.system.service.ISysConfigService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * 费用预存管理Service业务层处理
 * 
 * @author leige
 * @date 2023-10-27
 */
@Service
public class PayPrestoreServiceImpl implements IPayPrestoreService
{
    @Autowired
    private PayPrestoreMapper payPrestoreMapper;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private PayPrestoreAccountMapper payPrestoreAccountMapper;

    /**
     * 查询费用预存管理
     * 
     * @param id 费用预存管理主键
     * @return 费用预存管理
     */
    @Override
    public PayPrestore selectPayPrestoreById(Long id)
    {
        return payPrestoreMapper.selectPayPrestoreById(id);
    }

    /**
     * 查询费用预存管理列表
     * 
     * @param payPrestore 费用预存管理
     * @return 费用预存管理
     */
    @Override
    public List<PayPrestore> selectPayPrestoreList(PayPrestore payPrestore)
    {
        return payPrestoreMapper.selectPayPrestoreList(payPrestore);
    }

    /**
     * 新增费用预存管理
     * 
     * @param payPrestore 费用预存管理
     * @return 结果
     */
    @Override
    public int insertPayPrestorePay(PayPrestore payPrestore)
    {
        //设置类型
        payPrestore.setType(PmsConstants.PAY_PRESTORE_TYPE_PAY);
        //设置状态
        payPrestore.setState(PmsConstants.PAY_ORDER_STATE_0);
        //设置单号
        String prefix = sysConfigService.selectConfigByKey(PmsConstants.PREFIX_PRESTORE_YC);
        if (StringUtils.isBlank(prefix)) {
            throw new ServiceException("预存单号前缀没有配置");
        }
        payPrestore.setOrderNo(prefix+ IdUtil.getSnowflakeNextIdStr());
        payPrestore.setCreateTime(DateUtils.getNowDate());
        payPrestore.setCreateBy(SecurityUtils.getUsername());
        payPrestore.setUpdateBy(SecurityUtils.getUsername());
        payPrestore.setUpdateTime(new Date());
        return payPrestoreMapper.insertPayPrestore(payPrestore);
    }

    /**
     * 修改费用预存管理
     * 
     * @param payPrestore 费用预存管理
     * @return 结果
     */
    @Override
    public int updatePayPrestore(PayPrestore payPrestore)
    {
        payPrestore.setUpdateTime(DateUtils.getNowDate());
        return payPrestoreMapper.updatePayPrestore(payPrestore);
    }

    /**
     * 批量删除费用预存管理
     * 
     * @param ids 需要删除的费用预存管理主键
     * @return 结果
     */
    @Override
    public int deletePayPrestoreByIds(Long[] ids)
    {
        return payPrestoreMapper.deletePayPrestoreByIds(ids);
    }

    /**
     * 删除费用预存管理信息
     * 
     * @param id 费用预存管理主键
     * @return 结果
     */
    @Override
    public int deletePayPrestoreById(Long id)
    {
        return payPrestoreMapper.deletePayPrestoreById(id);
    }

    @Override
    @Transactional
    public int confirmPay(Long id) {
        //根据ID查询订单数据
        PayPrestore payPrestore = this.payPrestoreMapper.selectPayPrestoreById(id);
        if (ObjectUtils.isEmpty(payPrestore)) {
            throw new ServiceException("当前单据不存在");
        }
        //只能是订单生成状态的才能确定
        if (!payPrestore.getState().equals(PmsConstants.PAY_ORDER_STATE_0)) {
            throw new ServiceException("当前订单不是【订单生成】，不能进行确认");
        }
        Long feeUserId = payPrestore.getFeeUserId();//业主ID
        //根据业主ID查询账户表里面有没有这个业主的数据  如果有，直接加  没有 ，创建一个新的账号
        PayPrestoreAccount payPrestoreAccount = payPrestoreAccountMapper.queryPayPrestoreAccountByOwnerId(feeUserId);
        if (ObjectUtils.isEmpty(payPrestoreAccount)) {
            //创建
            PayPrestoreAccount prestoreAccount=new PayPrestoreAccount();
            prestoreAccount.setFeeUserId(feeUserId);
            prestoreAccount.setFeeUserName(payPrestore.getFeeUserName());
            prestoreAccount.setAmt(payPrestore.getAmt());
            prestoreAccount.setCreateBy(SecurityUtils.getUsername());
            prestoreAccount.setCreateTime(new Date());
            prestoreAccount.setUpdateTime(new Date());
            prestoreAccount.setUpdateBy(SecurityUtils.getUsername());
            //添加
            this.payPrestoreAccountMapper.insertPayPrestoreAccount(prestoreAccount);
        }else{
            //直接加
            payPrestoreAccount.setAmt(payPrestoreAccount.getAmt().add(payPrestore.getAmt()));
            //修改
            this.payPrestoreAccountMapper.updatePayPrestoreAccount(payPrestoreAccount);
        }
        //设置状态
        payPrestore.setState(PmsConstants.PAY_ORDER_STATE_2);
        return this.payPrestoreMapper.updatePayPrestore(payPrestore);
    }

    @Override
    public PayPrestoreAccount queryPayPrestoreAccountByUserOwnerId(Long id) {
        return this.payPrestoreAccountMapper.queryPayPrestoreAccountByOwnerId(id);
    }

    @Override
    public int addRefund(PayPrestore payPrestore) {
        payPrestore.setCreateTime(DateUtils.getNowDate());
        payPrestore.setCreateBy(SecurityUtils.getUsername());
        payPrestore.setType(PmsConstants.PAY_PRESTORE_TYPE_REFUND);
        payPrestore.setState(PmsConstants.PAY_ORDER_STATE_0);
        //生成orderNumber
        payPrestore.setOrderNo(IdUtil.getSnowflakeNextIdStr());
        //保存数据到pay_prestore
        int i = payPrestoreMapper.insertPayPrestore(payPrestore);
        return i;
    }

    @Override
    public int confirmRefund(Long id) {
        //根据ID查询pay_prestore里面的数据
        PayPrestore payPrestore=this.payPrestoreMapper.selectPayPrestoreById(id);
        if(null==payPrestore){
            throw new ServiceException("当前单号不存在");
        }
        //根据用户ID查询用户账号
        PayPrestoreAccount account=this.payPrestoreAccountMapper.queryPayPrestoreAccountByOwnerId(payPrestore.getFeeUserId());
        if(null==account){
            throw new ServiceException("业主没开户");
        }
        //退费的金额不能大于账号里面的金额
        if(account.getAmt().doubleValue()<payPrestore.getAmt().doubleValue()){
            throw new ServiceException("账号金额不足");
        }
        //更新pay_prestore的状态
        payPrestore.setState(PmsConstants.PAY_ORDER_STATE_5);
        this.payPrestoreMapper.updatePayPrestore(payPrestore);
        //修改账号金额
        account.setAmt(account.getAmt().subtract(payPrestore.getAmt()));
        return this.payPrestoreAccountMapper.updatePayPrestoreAccount(account);
    }
}
